/**
 * Copyright &copy; 2015-2020 <a href="www.dsesa.cn/">ESA</a> All rights reserved.
 */
package com.jeeplus.modules.sys.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sys.entity.DictValue;
import com.jeeplus.modules.sys.entity.LateFeePlan;
import com.jeeplus.modules.sys.service.DictValueService;
import com.jeeplus.modules.sys.service.LateFeePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xujin
 * @version 2020-01-09
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/lateFeePlan")
public class LateFeePlanController extends BaseController {

	@Autowired
	private LateFeePlanService lateFeePlanService;

	@Autowired
	private DictValueService dictValueService;

	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/sys/lateFeePlan/lateFeePlanList";
	}
	
	@ResponseBody
	@RequestMapping(value = "getLateFeePlan")
	public Map<String, Object> getLateFeePlan(@RequestParam(value = "objectId") String objectId) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(objectId == null || "".equals(objectId)){
			map.put("rows","[]");
			map.put("total",0);
		}else{
			List<LateFeePlan> list = lateFeePlanService.getList(objectId);
			map.put("rows",list);
			map.put("total", list.size());
		}
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value = "data")
	public Map<String, Object> data(DictValue dictValue, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<DictValue> page = dictValueService.findPage3(new Page<DictValue>(request, response), dictValue);
		return getBootstrapData(page);
	}


	@RequestMapping(value = "lateFeePlanForm")
	public String lateFeePlanForm(String lateFeePlanId, String objectId, Model model) {
		LateFeePlan lateFeePlan;
		if(lateFeePlanId == null || "".equals(lateFeePlanId)){
			lateFeePlan =  new LateFeePlan();
		}else{
			lateFeePlan = lateFeePlanService.get(lateFeePlanId);
		}

		lateFeePlan.setClassid(objectId);
		model.addAttribute("lateFeePlan", lateFeePlan);
		return "modules/sys/lateFeePlan/lateFeePlanForm";
	}


	@ResponseBody
	@RequestMapping(value = "save")
	public AjaxJson save(LateFeePlan lateFeePlan, Model model) {
		AjaxJson j = new AjaxJson();
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(lateFeePlan);
		if (StringUtils.isNotBlank(errMsg)){
			j.setSuccess(false);
			j.setMsg(errMsg);
			return j;
		}
		lateFeePlanService.save(lateFeePlan);
		j.setSuccess(true);
		j.setMsg("保存成功！");
		return j;
	}


	@ResponseBody
	@RequestMapping(value = "delete")
	public AjaxJson delete(String id) {
		AjaxJson j = new AjaxJson();
		lateFeePlanService.delete(new LateFeePlan(id));
		j.setSuccess(true);
		j.setMsg("删除键值成功！");
		return j;
	}

}
